package com.zdd.ehe.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdd.ehe.entity.DiseaseData;
import com.zdd.ehe.entity.ProvincePest;
import com.zdd.ehe.entity.Role;
import com.zdd.ehe.result.Result;
import com.zdd.ehe.service.ProvincePestService;
import com.zdd.ehe.vo.ProvincePestVOByYear;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.Year;
import java.util.List;

/**
 * @title:ProvincePestController
 * @author:ZhuiZhu
 * @description:
 * @createDate: 2023/3/24  16:40
 */
@Api(tags="各省市病虫害损失数据管理模块")
@RestController
@RequestMapping("/provincepest")
@Slf4j
public class ProvincePestController {
    @Resource
    private ProvincePestService provincePestService;

    @ApiOperation("分页获取各省市病虫害损失数据接口")
    @PostMapping("/getallprovincepestbypage")
    public Result getAllProvincePestByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        Page<ProvincePest> pagex = new Page<>(page,pageSize);
        Page<ProvincePest> pageResult = provincePestService.getAllProvincePestByPage(pagex);
        if(pageResult.getSize() == 0){
            return Result.error(4001,"无数据");
        }

        return Result.success("查询成功",pageResult);
    }

    @ApiOperation("获取某年所有病虫害造成的损失数据接口")
    @PostMapping("/getProvincePestByYear")
    public Result getProvincePestByYear(String year){
        //log.info("year: " + year);
        if(year == null){
            return Result.error(4002,"参数错误");
        }
        List<ProvincePestVOByYear> pestVOByYearList = provincePestService.getProvincePestByYear(Year.parse(year));
        if(pestVOByYearList.size() == 0){
            return Result.error(4001,"无数据");
        }

        return Result.success("查询成功",pestVOByYearList);
    }


    @ApiOperation("获取各年某种病虫害造成的损失数据接口")
    @PostMapping("/getProvincePestByDisease")
    public Result getProvincePestByDisease(@RequestParam("disease") String disease){

        List<ProvincePestVOByYear> pestVOByYearList = provincePestService.getProvincePestByDisease(disease);
        if(pestVOByYearList.size() == 0){
            return Result.error(4001,"无数据");
        }

        return Result.success("查询成功",pestVOByYearList);
    }

    @ApiOperation("添加病虫害损失数据信息")
    @PostMapping("/addProvincePest")
    public Result addProvincePest(@RequestBody ProvincePest provincePest){
        log.info("provincepest: " + provincePest);
        int d= provincePestService.addProvincePest(provincePest);
        if(d == 1){
            return Result.success("添加成功");
        }else {
            return Result.error(4002,"添加失败");
        }
    }

    @ApiOperation("删除病虫害数据损失信息接口")//有点问题
    @PostMapping("/deleteprovincepestbyid")
    public Result deleteProvincePestById(@RequestParam("PPestId") Integer PPestId){
        ProvincePest provincePest = provincePestService.getById(PPestId);
        if(provincePest == null){
            return Result.error(4001,"编号错误");
        }
        provincePest.setStatus("0");
        int result = provincePestService.update(provincePest);
        if(result == -1){
            return Result.error(4002,"删除失败");
        }
        return Result.success("删除成功");
    }

    @ApiOperation("重新启用病虫害数据损失信息状态接口")
    @PostMapping("/enableprovincepestbyid")
    public Result enableProvincePestById(@RequestParam("PPestId") Integer PPestId){
        ProvincePest provincePest = provincePestService.getById(PPestId);
        if(provincePest == null){
            return Result.error(4001,"编号错误");
        }
        provincePest.setStatus("1");
        int result = provincePestService.update(provincePest);
        if(result == -1){
            return Result.error(4002,"重新启用失败");
        }
        return Result.success("重新启用成功");
    }

    @ApiOperation("修改病虫害数据损失信息")
    @PostMapping("/updateprovincepest")
    public Result updateProvincePest(@RequestBody ProvincePest provincePest){
        int updateprovincepest = provincePestService.update(provincePest);
        if(updateprovincepest == -1){
            return Result.error("修改错误");
        }
        return Result.success("修改成功");
    }

    @ApiOperation("通过病虫害名称(disease_name)获取病虫害信息接口")
    @GetMapping("/getProvincePest")
    public Result getProvincePest(@RequestParam("diseaseName") String diseaseName){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("disease_name", diseaseName);
        List<ProvincePest> provincePestList = provincePestService.list(wrapper);
        return Result.success("查询成功",provincePestList);
    }
}
